﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UlsStudio.DesktopApp
{
    public class QuickSearchToolStripTextBox : ToolStripTextBox
    {
        string originalSearchText = string.Empty;       // Populated on form load

        public QuickSearchToolStripTextBox() 
        {
            this.Enter += QuickSearchToolStripTextBox_Enter;
            this.Leave += QuickSearchToolStripTextBox_Leave;
            this.KeyPress += QuickSearchToolStripTextBox_KeyPress;
        }

        

        public string OriginalSearchText
        {
            get { return originalSearchText; }
            set { originalSearchText = value; }
        }

        #region Event Definitions

        public delegate void NewSearchWantedDelegate(object sender, EventArgs e);
        public event NewSearchWantedDelegate NewSearchWanted = null;
        private void OnNewSearchWanted()
        {
            if (NewSearchWanted != null)
            {
                NewSearchWanted(this, EventArgs.Empty);
            }
        }

        #endregion

        #region Event Handlers

        void QuickSearchToolStripTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                OnNewSearchWanted();
                e.Handled = true;
            }
        }

        void QuickSearchToolStripTextBox_Leave(object sender, EventArgs e)
        {
            if (this.Text.Trim().Length == 0)
            {
                // Reset to "Enter Quick Search Here"
                this.Text = this.OriginalSearchText;
                this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.ForeColor = System.Drawing.Color.DarkGray;
            }
        }

        void QuickSearchToolStripTextBox_Enter(object sender, EventArgs e)
        {
            if (this.Text == this.OriginalSearchText)
            {
                this.Text = string.Empty;
                this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.ForeColor = System.Drawing.SystemColors.WindowText;
            }
        }
        #endregion

    }
}
